package com.example.patternfactory0307.strategy;

import com.google.common.collect.Maps;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Component
public class ActionLogStrategyHolder implements InitializingBean {
    private Map<String, ActionLogStrategy> actionLogStrategyMap = Maps.newConcurrentMap();

    @Autowired
    private List<ActionLogStrategy> actionLogStrategyList;

    public ActionLogStrategy getBy(String BizType) {
        return actionLogStrategyMap.get(BizType);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if(CollectionUtils.isEmpty(actionLogStrategyList)){
            return;
        }
//        actionLogStrategyList.forEach(actionLogStrategy -> {
//            actionLogStrategyMap.put(actionLogStrategy.getBizTypeStrategy(), actionLogStrategy);
//        });

        for (int i = 0 ; i < actionLogStrategyList.size() ; i++){
            ActionLogStrategy actionLogStrategy = actionLogStrategyList.get(i);
            String bizTypeStrategy = actionLogStrategy.getBizTypeStrategy();
            actionLogStrategyMap.put(bizTypeStrategy,actionLogStrategy);
        }
        System.out.println("------");
    }
}
